﻿@model GolfLeague.EditModels.RoundsEditModel

<script type="text/javascript">
    $(document).ready(function () {
        $('#dialog').dialog({
            autoOpen: false,
            model: true
        });
        $('#NewRound_RoundDate').datepicker();
        $('#RoundBatchOptions_StartDate').datepicker();
        $('input:button').button();
        $('input:reset').button();
        $('input:submit').button();
        document.getElementById('RoundBatchOptions_StartTime').disabled = true;
        document.getElementById('RoundBatchOptions_MatchOffset').disabled = true;
        document.getElementById('RoundBatchOptions_SelectedTeams').disabled = true;
        document.getElementById('SelectAllTeams').disabled = true;
        $('div#buttonlist a').hover(
		    function () { $(this).addClass('ui-state-hover'); },
		    function () { $(this).removeClass('ui-state-hover'); }
	    );
    });

    $(function () {
        $('.DeleteButton').click(function (e) {
            e.preventDefault();
            var recordToDelete = $(this).attr('id');
            recordToDelete = recordToDelete.replace('delete-', '');

            $('#dialog').dialog('option', 'buttons', {
                'Yes': function () {
                    $(this).dialog('close');
                    if (recordToDelete != '') {
                        $.post('/Round/RoundDelete', { 'roundID': recordToDelete },
                        function (data) {
                            $('#row-' + recordToDelete).fadeOut('slow');
                        });
                    }
                },
                'No': function () {
                    $(this).dialog('close');
                    return false;
                }
            });
            $('#dialog').dialog('open');
        });
    });

    $(function () {
        $('.EditButton').click(function (e) {
            e.preventDefault();
            var recordToEdit = $(this).attr('id');
            recordToEdit = recordToEdit.replace('edit-', '');

            if (recordToEdit != '') {
                window.location = '/Round/RoundEdit/' + recordToEdit;
            }
        });
    });

    $(function () {
        $('.CancelEditButton').click(function (e) {
            e.preventDefault();
            var scheduleID = $(this).attr('id');
            scheduleID = scheduleID.replace('cancel-', '');

            if (scheduleID != '') {
                window.location = '/Round/' + scheduleID;
            }
        });
    });

    $(function () {
        $('.MatchesButton').click(function (e) {
            e.preventDefault();
            var matchID = $(this).attr('id');
            matchID = matchID.replace('match-', '');

            if (matchID != '') {
                window.location = '/Match/Index/' + matchID;
            }
        });
    });

    $(function () {
        $('.CourseDropdown').change(function () {
            var courseID = $(this).val();
            var options = '';
            var teeBoxDropdownID = ($(this).attr('id') == 'NewRound_CourseID' ? '#NewRound_TeeBoxID' : '#RoundBatchOptions_TeeBoxID');

            if (courseID != '') {
                $.ajax({
                    url: '/Round/RoundTeeBoxList',
                    type: 'POST',
                    dataType: 'json',
                    data: '{ courseID : "' + courseID + '"}',
                    contentType: 'application/json; charset=utf-8',
                    success: function (data) {
                        $.each(data.teeBoxDropdown, function (i, value) {
                            options += '<option value="' + value.TeeBoxID + '">' + value.TeeBoxColor + '</option>';
                        });
                        if (options == '') options = '<option value="0">No TeeBoxes Found</option>';
                        $(teeBoxDropdownID).html(options);
                    }
                });
            }
        });
    });

    $(function () {
        $('.SelectAllTeams').click(function (e) {
            var selectList = document.getElementById('RoundBatchOptions_SelectedTeams');
            var selectOption = $(this).attr('checked');
            //if ($(this).checked) == true) selectOption = true;

            for (var i = 0, length = selectList.options.length, option; i < length; i++) {
                selectList.options[i].selected = selectOption;
            }
        });
    });

    $(function () {
        $('.CreateMatches').click(function (e) {
            var checkBox = document.getElementById('CreateMatches');
            var enableOption = !($(this).attr('checked'));
            document.getElementById('RoundBatchOptions_StartTime').disabled = enableOption;
            document.getElementById('RoundBatchOptions_MatchOffset').disabled = enableOption;
            document.getElementById('RoundBatchOptions_SelectedTeams').disabled = enableOption;
            document.getElementById('SelectAllTeams').disabled = enableOption;
        });
    });

    $(function () {
        $(".BatchButton").click(function (e) {
            var selectList = document.getElementById('RoundBatchOptions_SelectedTeams');
            if (selectList.disabled == false) {
                var selectCount = 0;
                for (var i = 0, length = selectList.options.length, option; i < length; i++) {
                    if (selectList.options[i].selected == true) selectCount++;
                }

                if (selectCount % 2 == 0 && selectCount > 0)
                    return true;
                else {
                    alert('You must select an even number of teams.');
                    return false;
                }
            }
        });
    });

</script>

@{
    ViewBag.Title = "Rounds";
}
@Html.ActionLink("Admin Home", "Index") > @Html.ActionLink("Schedules", "Index", "Schedule") > Rounds
<table class="formTable" width="50%">
    <tr>
        <td class="formTable">
@using (Html.BeginForm()) {
    <fieldset>
        <legend>Add Date</legend>

        @Html.HiddenFor(model => model.NewRound.ScheduleID)
        @Html.HiddenFor(model => model.NewRound.RoundID)

        <div class="editor-label">
            @Html.LabelFor(model => model.NewRound.RoundDate)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.NewRound.RoundDate)
            @Html.ValidationMessageFor(model => model.NewRound.RoundDate)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.NewRound.Course.CourseName)
        </div>
        <div class="editor-field">
            @Html.DropDownListFor(model => model.NewRound.CourseID, new SelectList(Model.Courses, "CourseID", "CourseName", Model.NewRound.CourseID), new { @class = "CourseDropdown" })
            @Html.ValidationMessageFor(model => model.NewRound.CourseID)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.NewRound.TeeBox.TeeBoxName)
        </div>
        <div class="editor-field">
            @Html.DropDownListFor(model => model.NewRound.TeeBoxID, new SelectList(Model.TeeBoxes, "dropdownID", "dropdownText", Model.NewRound.SideID))
            @Html.ValidationMessageFor(model => model.NewRound.TeeBoxID)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.NewRound.Side.SideName)
        </div>
        <div class="editor-field">
            @Html.DropDownListFor(model => model.NewRound.SideID, new SelectList(Model.Sides, "SideID", "SideName", Model.NewRound.SideID))
            @Html.ValidationMessageFor(model => model.NewRound.SideID)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.ScoringMethods)
        </div>
        <div class="editor-field">
            @Html.ListBoxFor(model => model.ScoringMethods, new SelectList(Model.ScoringMethodsDropdown, "dropdownID", "dropdownText", Model.ScoringMethods))
            @Html.ValidationMessageFor(model => model.ScoringMethods)
        </div>        

        <p>
        @if (Model.NewRound.RoundID < 1)
        {
            <input type="submit" value="Add" />
        }
        else
        {
            <input type="submit" value="Save" />
            <input type="reset" class="CancelEditButton" id="cancel-@Model.Schedule.ScheduleID" value="Cancel" />
        }
        </p>
        <div id="dialog" title="Confirm Delete">Are you sure you want to delete this round and all of its matches?</div>
    </fieldset>
}
        </td>
        <td class="formTable">
@using (Html.BeginForm("RoundBatch", "Round"))
{
    @Html.HiddenFor(model => model.Schedule.ScheduleID)
<fieldset>
    <legend>Batch Create</legend>
    <table class="formTable">
        <tr>
            <td class="formTable">
                <fieldset>
                    <legend class="sublegend">Round Options</legend>
                    <div class="editor-label">
                        @Html.LabelFor(model => model.RoundBatchOptions.StartDate)
                    </div>
                    <div class="editor-field">
                        @Html.EditorFor(model => model.RoundBatchOptions.StartDate)
                        @Html.ValidationMessageFor(model => model.RoundBatchOptions.StartDate)
                    </div>

                    <div class="editor-label">
                        @Html.LabelFor(model => model.RoundBatchOptions.NumberOfRounds)
                    </div>
                    <div class="editor-field">
                        @Html.EditorFor(model => model.RoundBatchOptions.NumberOfRounds)
                        @Html.ValidationMessageFor(model => model.RoundBatchOptions.NumberOfRounds)
                    </div>

                    <div class="editor-label">
                        @Html.LabelFor(model => model.RoundBatchOptions.Frequency)
                    </div>
                        <div class="editor-field">
                        @Html.DropDownListFor(model => model.RoundBatchOptions.Frequency, new SelectList(Model.RoundBatchOptions.FrequencyDropDown, "dropdownID", "dropdownText"))
                        @Html.ValidationMessageFor(model => model.RoundBatchOptions.Frequency)
                    </div>
        
                    <div class="editor-label">
                        @Html.LabelFor(model => model.RoundBatchOptions.CourseID)
                    </div>
                    <div class="editor-field">
                        @Html.DropDownListFor(model => model.RoundBatchOptions.CourseID, new SelectList(Model.Courses, "CourseID", "CourseName"), new { @class = "CourseDropdown" })
                        @Html.ValidationMessageFor(model => model.RoundBatchOptions.CourseID)
                    </div>

                    <div class="editor-label">
                        @Html.LabelFor(model => model.RoundBatchOptions.TeeBoxID)
                    </div>
                    <div class="editor-field">
                        @Html.DropDownListFor(model => model.RoundBatchOptions.TeeBoxID, new SelectList(Model.TeeBoxes, "dropdownID", "dropdownText"))
                        @Html.ValidationMessageFor(model => model.RoundBatchOptions.TeeBoxID)
                    </div>

                    <div class="editor-label">
                        @Html.LabelFor(model => model.RoundBatchOptions.StartingSide)
                    </div>
                    <div class="editor-field">
                        @Html.DropDownListFor(model => model.RoundBatchOptions.StartingSide, new SelectList(Model.Sides, "SideID", "SideName"))
                        @Html.ValidationMessageFor(model => model.RoundBatchOptions.StartingSide)
                    </div>

                    <div class="editor-label">
                        @Html.LabelFor(model => model.RoundBatchOptions.RoundScoringMethods)
                    </div>
                    <div class="editor-field">
                        @Html.ListBoxFor(model => model.RoundBatchOptions.RoundScoringMethods, new SelectList(Model.ScoringMethodsDropdown, "dropdownID", "dropdownText", Model.RoundBatchOptions.RoundScoringMethods), new { size = 5 })
                        @Html.ValidationMessageFor(model => model.RoundBatchOptions.RoundScoringMethods)
                    </div>    
                </fieldset>
                <p>
                    <input type="submit" class="BatchButton" value="Create" />
                </p>

            </td>
            <td class="formTable">
                @Html.CheckBoxFor(model => model.RoundBatchOptions.CreateMatches, new { @class = "CreateMatches" })Create Matches?
                <fieldset>
                    <legend class="sublegend">Match Options</legend>
                    <div class="editor-label">
                        @Html.LabelFor(model => model.RoundBatchOptions.StartTime)
                    </div>
                    <div class="editor-field">
                        @Html.EditorFor(model => model.RoundBatchOptions.StartTime)
                        @Html.ValidationMessageFor(model => model.RoundBatchOptions.StartTime)
                    </div>

                    <div class="editor-label">
                        @Html.LabelFor(model => model.RoundBatchOptions.MatchOffset)
                    </div>
                    <div class="editor-field">
                        @Html.EditorFor(model => model.RoundBatchOptions.MatchOffset)
                        @Html.ValidationMessageFor(model => model.RoundBatchOptions.MatchOffset)
                    </div>

                    <div class="editor-label">
                        @Html.LabelFor(model => model.RoundBatchOptions.SelectedTeams)<input id="SelectAllTeams" class="SelectAllTeams" type="checkbox" name="SelectAllTeams" />Select All
                    </div>
                    <div class="editor-field">
                        @Html.ListBoxFor(model => model.RoundBatchOptions.SelectedTeams, new SelectList(Model.RoundBatchOptions.TeamList, "dropdownID", "dropdownText"), new { size = 10 })
                        @Html.ValidationMessageFor(model => model.RoundBatchOptions.SelectedTeams)
                    </div>
                </fieldset>
            </td>
        </tr>
    </table>
</fieldset>
}
        </td>
    </tr>
</table>
<fieldset>
    <legend>Schedule Dates for @Model.Schedule.ScheduleName</legend>
    <table>
        <tr>
            <th>
                Date
            </th>
            <th>
                Course
            </th>
            <th>
                Side
            </th>
            <th>
                TeeBox
            </th>
            <th># of Matches</th>
            <th></th>
        </tr>

@foreach (var item in Model.Rounds)
{
        <tr id="row-@item.RoundID">
            <td>
                @String.Format("{0:MM/dd/yyyy}", item.RoundDate)
            </td>
            <td>
                @item.Course.CourseName
            </td>
            <td>
                @item.Side.SideName
            </td>
            <td>
                @item.TeeBox.TeeBoxName
            </td>
            <td align="center">
                @item.Matches.Count
            </td>
            <td valign="top">
                <input type="button" class="MatchesButton" id="match-@item.RoundID" value= "Matches" />
                <div id="buttonlist" style="vertical-align:middle">
                    <a href="#" title="Edit Round" class="EditButton ui-state-default ui-corner-all ui-icon ui-icon-pencil" id="edit-@item.RoundID"></a>
                    <a href="#" title="Delete Round" class="DeleteButton ui-state-default ui-corner-all ui-icon ui-icon-trash" id="delete-@item.RoundID"></a>
                </div>
            </td>
        </tr>
}
    </table>
</fieldset>
@Html.ActionLink("Back to Schedules", "Schedules")